﻿// <author>Diogo Silva 11238</author>
// <author>Raúl Gonçalo Palma 11392</author>
// <date>16/12/2014</date>

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace CSharpProject
{
    /// <summary>
    ///  Classe resposável por ler os dados da folha de cálculo.
    /// </summary>
    class ReadXlsFile
    {
        
        /// <summary>
        ///  Construtor da classe ReadXlsFile.
        /// </summary>
        public ReadXlsFile()
        {
        }

        /// <summary>
        ///  Lê o ficheiro xls linha a linha e coloca numa lista
        ///  param name="workbookPath" - Caminho para o ficheiro xls
        ///  returns Retorna uma lista com as linhas da folha de cálculo
        /// </summary>
        public List<XlsLine> ReadXls(string workbookPath)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            Excel.Range range ;
            List<XlsLine> values = new List<XlsLine>();
           

            xlApp = new Excel.Application() ;
            xlWorkBook = xlApp.Workbooks.Open(workbookPath, 0, true, 5, "", "", true, 
                Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
          
            range = xlWorkSheet.UsedRange;

            for (int i = 2; i <= 38; i++)
            {
                int ano = Convert.ToInt32((range.Cells[i, 2] as Excel.Range).Value2);
                double ipcGlobal = Convert.ToDouble((range.Cells[i, 3] as Excel.Range).Value2);
                double variacaoAnual = Convert.ToDouble((range.Cells[i, 4] as Excel.Range).Value2);
                double remunMinimaMensal = Convert.ToDouble((range.Cells[i, 5] as Excel.Range).Value2);
                double remunMaximaMensal = Convert.ToDouble((range.Cells[i, 6] as Excel.Range).Value2);
                double pib = Convert.ToDouble((range.Cells[i, 7] as Excel.Range).Value2);
                double rendiNacionalBruto = Convert.ToDouble((range.Cells[i, 8] as Excel.Range).Value2);
                double renDisponivelBrutoAnual = Convert.ToDouble((range.Cells[i, 9] as Excel.Range).Value2);
                double remunPerCapitaAnual = Convert.ToDouble((range.Cells[i, 10] as Excel.Range).Value2);

                XlsLine line = new XlsLine(ano, ipcGlobal, variacaoAnual, remunMinimaMensal, remunMaximaMensal, pib,
                    rendiNacionalBruto, renDisponivelBrutoAnual, remunPerCapitaAnual);
                values.Add(line);

            }
            xlWorkBook.Close(false, null, null);
            xlApp.Quit();

            return values;
        }
    }
}
